package com.cskaoyan.wx.service;

import com.cskaoyan.bean.*;

import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.wx.bean.vo.CollectListVo;
import com.cskaoyan.wx.bean.vo.WxAddOrDeleteVo;
import com.cskaoyan.wx.bean.vo.WxCollectListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;

/**
 * @ description:
 * @ author:台
 **/
/**
 * @auther hhy
 */

@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    GoodsMapper goodsMapper;

    Integer id;
    String username;

    @Override
    public CollectListVo collectList(Byte type, Integer page, Integer size) {
        PageHelper.startPage(page,size);

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();

        if (id == null || username == null) {
            getIdAndUsername();
        }
        if (id == null || username == null) {
            return new CollectListVo();
        }

        criteria.andUserIdEqualTo(id);
        criteria.andTypeEqualTo(type);

        List<Collect> collects = collectMapper.selectByExample(collectExample);

        List<WxCollectListVo> wxCollectListVos = new ArrayList<>();

        for (Collect collect : collects) {
            Integer valueId = collect.getValueId();
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
            criteria1.andIdEqualTo(valueId);
            List<Goods> goods = goodsMapper.selectByExample(goodsExample);

            Goods goods1 = goods.get(0);
            WxCollectListVo wxCollectListVo = new WxCollectListVo();

            wxCollectListVo.setBrief(goods1.getBrief());
            wxCollectListVo.setId(collect.getId());
            wxCollectListVo.setName(goods1.getName());
            wxCollectListVo.setPicUrl(goods1.getPicUrl());
            wxCollectListVo.setRetailPrice(goods1.getRetailPrice());
            wxCollectListVo.setType(type);
            wxCollectListVo.setValueId(collect.getValueId());

            wxCollectListVos.add(wxCollectListVo);
        }

        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();

        CollectListVo collectListVo = new CollectListVo();
        collectListVo.setCollectList(wxCollectListVos);
        collectListVo.setTotalPages(total);

        return collectListVo;
    }

    private void getIdAndUsername() {
        Subject subject = SecurityUtils.getSubject();
        username = (String) subject.getPrincipal();
        if (username == null)return;
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        id = users.get(0).getId();
    }

    @Override
    public WxAddOrDeleteVo addOrDelete(Byte type, Integer valueId) {
        if (id == null || username == null) {
            getIdAndUsername();
        }
        if (id == null || username == null) {
            return new WxAddOrDeleteVo();
        }
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);

        WxAddOrDeleteVo wxAddOrDeleteVo = new WxAddOrDeleteVo();
        Date date = new Date();

        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() == 0) {
            wxAddOrDeleteVo.setType("add");
            Collect collect = new Collect();
            collect.setAddTime(date);
            collect.setType(type);
            collect.setUpdateTime(date);
            collect.setUserId(id);
            collect.setValueId(valueId);
            collectMapper.insertSelective(collect);
            return wxAddOrDeleteVo;
        } else {
            wxAddOrDeleteVo.setType("delete");
            Collect collect = new Collect();
            collect.setDeleted(true);
            collectMapper.updateByExampleSelective(collect, collectExample);
            return wxAddOrDeleteVo;
        }
    }
}
